What is cli-ux?
The cli-ux npm package provides a set of utilities for building command-line interfaces (CLIs). It offers features such as progress bars, spinners, prompts, and table formatting to enhance the user experience of CLI applications.
What are cli-ux's main functionalities?
Progress Bar
This feature allows you to display a progress bar in the terminal. The code sample demonstrates how to create and update a progress bar that increments by 5% every 100 milliseconds until it reaches 100%.
const cli = require('cli-ux');
const progress = cli.progress({ format: 'progress [{bar}] {percentage}% | ETA: {eta}s' });
progress.start(100, 0);
let value = 0;
const interval = setInterval(() => {
value += 5;
progress.update(value);
if (value >= 100) {
clearInterval(interval);
progress.stop();
}
}, 100);
Spinner
This feature provides a spinner to indicate ongoing processes. The code sample shows how to start a spinner with a message and stop it after 3 seconds with a 'done' message.
const cli = require('cli-ux');
cli.action.start('Processing');
setTimeout(() => {
cli.action.stop('done');
}, 3000);
Prompt
This feature allows you to prompt the user for input. The code sample demonstrates how to ask the user for their name and then greet them with the provided input.
const cli = require('cli-ux');
(async () => {
const name = await cli.prompt('What is your name?');
console.log(`Hello, ${name}!`);
})();
Table
This feature allows you to display data in a table format. The code sample shows how to create a table with two columns: 'name' and 'age', and display two rows of data.
const cli = require('cli-ux');
const data = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 }
];
cli.table(data, {
name: {
minWidth: 7
},
age: {
header: 'Age'
}
});
Other packages similar to cli-ux
ora
Ora is a lightweight and elegant terminal spinner. It provides similar functionality to the spinner feature in cli-ux but focuses solely on spinners, offering more customization options and better performance.
inquirer
Inquirer is a powerful library for creating interactive command-line prompts. It offers more advanced and flexible prompt types compared to cli-ux, making it a better choice for complex user interactions.
blessed
Blessed is a comprehensive library for building terminal interfaces. It provides a wide range of widgets, including tables, forms, and more, offering greater flexibility and customization than cli-ux.
progress
Progress is a simple and flexible progress bar library for Node.js. It offers similar functionality to the progress bar feature in cli-ux but with a more minimalistic approach and fewer dependencies.
cli-ux
cli IO utilities
Usage
The following assumes you have installed cli-ux
to your project with npm install cli-ux
or yarn add cli-ux
and have it required in your script (TypeScript example):
import cli from 'cli-ux'
cli.prompt('What is your name?')
JavaScript:
const {cli} = require('cli-ux')
cli.prompt('What is your name?')
cli.prompt()
Prompt for user input.
await cli.prompt('What is your name?')
await cli.prompt('What is your two-factor token?', {type: 'mask'})
await cli.prompt('What is your password?', {type: 'hide'})
await cli.confirm('Continue?')
await cli.anykey()
cli.url(text, uri)
Create a hyperlink (if supported in the terminal)
await cli.url('sometext', 'https://google.com')
cli.open
Open a url in the browser
await cli.open('https://oclif.io')
cli.action
Shows a spinner
cli.action.start('starting a process')
cli.action.start('starting a process', {stdout: true})
cli.action.stop()
cli.action.stop('custom message')
This degrades gracefully when not connected to a TTY. It queues up any writes to stdout/stderr so they are displayed above the spinner.
cli.annotation
Shows an iterm annotation
cli.annotation('sometest', 'annotated with this text')
cli.wait
Waits for 1 second or given milliseconds
await cli.wait()
await cli.wait(3000)
cli.tree
Generate a tree and display it
let tree = cli.tree()
tree.insert('foo')
tree.insert('bar')
let subtree = cli.tree()
subtree.insert('qux')
tree.nodes.bar.insert('baz', subtree)
tree.display()
Outputs:
├─ foo
└─ bar
└─ baz
└─ qux